/**
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved. 
 */
package com.wilmar.process.dao.impl;

import java.text.SimpleDateFormat;
import java.util.List;

import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.dao.api.ISubProcessInstanceRelationDAO;
import com.wilmar.process.entity.SubProcessInstanceRelation;
import com.wilmar.process.entity.query.SubProcessinstanceRelationQueryObject;
import com.wilmar.process.entity.query.SubProcessinstanceRelationQueryObject.RelationCondition;

/**
 * @author makubex Email:makubexnet@gmail.com
 * @version 1.0.0 Jun 17, 2010
 * @since 1.0.0
 */
public class SubProcessInstanceRelationDAOImpl extends
		BaseDAOImpl<SubProcessInstanceRelation, Long> implements
		ISubProcessInstanceRelationDAO {

	/**
	 * @see com.wilmar.process.dao.api.ISubProcessInstanceRelationDAO#getListByQueryObject(com.wilmar.process.entity.query.SubProcessinstanceRelationQueryObject)
	 */
	public List<SubProcessInstanceRelation> getListByQueryObject(
			SubProcessinstanceRelationQueryObject queryObject) {
		StringBuffer hql = new StringBuffer();
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_SELECT);
		hql.append("relation_");
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_FROM);
		hql.append(SubProcessInstanceRelation.class.getName());
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AS);
		hql.append("relation_");
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_WHERE);
		hql.append("1=1");
		SimpleDateFormat sdf = new SimpleDateFormat(
				ProcessSystemDeclareKeys.SYSTEM_LONG_DATE_FORMAT
						+ ProcessSystemDeclareKeys.SPACE
						+ ProcessSystemDeclareKeys.SYSTEM_LONG_TIME_FORMAT);
		if (queryObject.getMaxCreateTime() != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("relation_.createTime");
			hql.append(ProcessSystemDeclareKeys.LESS_EQUAL);
			hql.append("to_date(");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(sdf.format(queryObject.getMaxCreateTime()));
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("yyyy-mm-dd hh24:mi:ss");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(")");
		}
		if (queryObject.getMinCreateTime() != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("relation_.createTime");
			hql.append(ProcessSystemDeclareKeys.GREATER_EQUAL);
			hql.append("to_date(");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(sdf.format(queryObject.getMinCreateTime()));
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("yyyy-mm-dd hh24:mi:ss");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(")");
		}
		if (queryObject.getRelationIds() != null
				&& queryObject.getRelationIds()[0] != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("relation_.id");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (Long id : queryObject.getRelationIds()) {
				hql.append(id);
				hql.append(ProcessSystemDeclareKeys.COMMA);
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (queryObject.getPsRelationMap() != null
				&& queryObject.getPsRelationMap()[0] != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (RelationCondition condition : queryObject.getPsRelationMap()) {
				if (condition.getParentExid() != null
						|| condition.getSubExid() != null) {
					hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
					if (condition.getParentExid() != null) {
						hql.append("relation_.parentExid");
						hql.append(ProcessSystemDeclareKeys.EQUAL);
						hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
						hql.append(condition.getParentExid());
						hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					}
					if (condition.getParentExid() != null
							&& condition.getSubExid() != null)
						hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
					if (condition.getSubExid() != null) {
						hql.append("relation_.subExid");
						hql.append(ProcessSystemDeclareKeys.EQUAL);
						hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
						hql.append(condition.getSubExid());
						hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					}
					hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
					hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_OR);
				}
			}
			hql.delete(hql.length()
					- ProcessSystemDeclareKeys.SQL_STATEMENT_OR.length(), hql
					.length());
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		return super.getListByHql(hql.toString());
	}

}
